<?php


namespace App\Http\Controllers;


use App\Models\Customer;
use App\Models\Distribution;
use Exception;

class WebController extends Controller
{
    /**
     * @return false|string
     */
    public function manage()
    {
        return file_get_contents(public_path('manage.html'));
    }

    /**
     * @param $id
     * @return mixed
     */
    public function customer($id)
    {
        try {
            $customer = Customer::query()->findOrFail($id);
            $html = file_get_contents(public_path('customer.html'));
            $string = '<script type="text/javascript">const CUSTOMER = ' . $id . ';</script>';
            $html = str_replace('[RE]', $string, $html);
            return str_replace('[CUSTOMER]', $customer->getAttribute('name'), $html);
        } catch (Exception $exception) {
            abort(404);
        }
    }

    /**
     * @param $id
     * @return mixed
     * @author luffyzhao@vip.126.com
     */
    public function distribution($id)
    {
        try {
            $distribution = Distribution::query()->findOrFail($id);
            $html = file_get_contents(public_path('distribution.html'));
            $string = '<script type="text/javascript">const DISTRIBUTION = ' . $id . ';</script>';
            $html = str_replace('[RE]', $string, $html);
            return str_replace('[DISTRIBUTION]', $distribution->getAttribute('name'), $html);
        } catch (Exception $exception) {
            abort(404);
        }
    }
}
